package com.fhyc.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fhyc.common.Result;
import com.fhyc.util.Base64Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/fm/login")
public class LoginController {

    @GetMapping("/verify")
    public Result parseToken(HttpServletRequest request) {
        String accessToken = "";
        if (StringUtils.isNotEmpty(request.getHeader("accessToken"))){
            accessToken    = request.getHeader("accessToken");
        }else {
            accessToken = request.getHeader("Access-Token");
        }
        //log.info("解析前:{}",accessToken);
        if (accessToken != null && !"".equals(accessToken) && !"null".equals(accessToken)) {
            try {
                String strTokenclaims = accessToken.split("\\.")[1];
                String strCalimsJson = Base64Util.decodeToString(strTokenclaims);
                log.info("accessToken解析后:{}",strCalimsJson);
                ObjectMapper objMapper = new ObjectMapper();
                return Result.ok(objMapper.readValue(strCalimsJson, Map.class));
            } catch (Exception var4) {
                log.error("解析token发生错误。token={}", accessToken, var4);
                return Result.error("解析token发生错误:" + var4);
            }
        } else {
            return null;
        }
    }

}
